package kdweibo4j;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import kdweibo4j.http.Response;
import kdweibo4j.org.json.JSONArray;
import kdweibo4j.org.json.JSONException;
import kdweibo4j.org.json.JSONObject;

import com.kdweibo.utils.Utils;



/**
 * 话题
 * 
 * @author SinaWeibo
 * @since Weibo4J 2.0.3
 */
public class Topic extends WeiboResponse {
	public Date createdDAt;
	private String id;
	private boolean hot;
	private boolean Nnew;
	private String tagId = null;
	private String name = null;
	private String status = null;
	private String status_username = null;
	private String internalAd;
	private static final long serialVersionUID = 1925956704460743946L;

	public Topic() {
		super();
	}

	public Topic(Response res) throws WeiboException {
		super(res);
		JSONObject json = res.asJSONObject();
		try {
			id = json.getString("id");
			hot = json.getBoolean("hot");
			Nnew = json.getBoolean("new");
			tagId = json.getString("tagId");
			name = json.getString("name");
			status = json.getJSONObject("status").getString("text");
			status_username = json.getJSONObject("status")
					.getJSONObject("user").getString("screen_name");
			createdDAt = Utils.parseDate(json.getString("createTime"),
					"EEE MMM dd HH:mm:ss z yyyy");
			if (json.getString("id") != null)
				id = json.getString("id");
		} catch (JSONException je) {
			throw new WeiboException(je.getMessage() + ":" + json.toString(),
					je);
		}
	}

	public Topic(JSONObject json) throws WeiboException {
		try {
			id = json.getString("id");
			hot = json.getBoolean("hot");
			Nnew = json.getBoolean("new");
			tagId = json.getString("tagId");
			name = json.getString("name");
			// "internalAd":"http://t.kingdee.com/microblog/filesvr/4e283d58cce771d07a49d73d"
			internalAd = json.getString("internalAd");
			try {
				status = json.getJSONObject("status").getString("text");

				status_username = json.getJSONObject("status").getJSONObject(
						"user").getString("screen_name");
//				Log.d("topic", json.getString("created_at"));
				createdDAt =parseDate(json.getJSONObject("status").getString("created_at"),"EEE MMM dd HH:mm:ss z yyyy");
			} catch (Exception excp) {

			}
			// createdDAt = parseDate(json.getString("createTime"),
			// "EEE MMM dd HH:mm:ss z yyyy");
		} catch (JSONException je) {
			throw new WeiboException(je.getMessage() + ":" + json.toString(),
					je);
		}
	}

	static List<Topic> constructTopicList(Response res) throws WeiboException {
		try {
			JSONArray list = res.asJSONArray();
			
			int size = list.length();
			List<Topic> topics = new ArrayList<Topic>(size);
			for (int i = 0; i < size; i++) {
//				Log.d("topic", "topic"+list.getJSONObject(i));
				topics.add(new Topic(list.getJSONObject(i)));
			}
			return topics;
		} catch (JSONException jsone) {
			throw new WeiboException(jsone);
		} catch (WeiboException te) {
			throw te;
		}

	}

	static int constructTopicCount(Response res) throws WeiboException {

		JSONObject obj = res.asJSONObject();
		int count = 0;
		try {
			count = obj.getInt("followed_topic_num");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;

	}

	@Override
	public String toString() {
		return "Topic [id=" + id + ", hot=" + hot + ", new=" + Nnew
				+ ", tagId=" + tagId + ", name=" + name + ", status=" + status
				+ ", status_username=" + status_username + "]";
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setInternalAd(String internalAd) {
		this.internalAd = internalAd;
	}

	public String getinternalAd() {
		return internalAd;
	}

	public boolean isHot() {
		return hot;
	}

	public void setHot(boolean hot) {
		this.hot = hot;
	}

	public boolean isNnew() {
		return Nnew;
	}

	public void setNnew(boolean nnew) {
		Nnew = nnew;
	}

	public Date getCreatedAt() {
		return this.createdDAt;
	}

	public String getTagId() {
		return tagId;
	}

	public void setTagId(String tagId) {
		this.tagId = tagId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public String getStatus() {
		return status;
	}

	public String getStatus_username() {
		return status_username;
	}

}